package com.ruoyi.flowable.listener.scrap;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.equ.domain.EquScrap;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 报废-2-2监听：资产管理部门
 */
@Slf4j
@Component("scrapListener_2_2")
public class ScrapListener_2_2 implements ExecutionListener {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void notify(DelegateExecution execution) {
        try {
            //资产管理部门
            String flag3 = execution.getVariable("flag3", String.class);
            String checkRes3 = execution.getVariable("checkRes3", String.class);
            String proposal3 = execution.getVariable("proposal3", String.class);
            HashMap<String, Object> assetMap = new HashMap<>();
            assetMap.put("确认设备状况与台账一致", flag3);
            assetMap.put("核查结果", checkRes3);
            assetMap.put("修复建议", proposal3);

            EquScrap equScrap = execution.getVariable("equScrap", EquScrap.class);

            HashMap<String, Object> approvalSummaryMap = objectMapper.readValue(equScrap.getApprovalSummary(), new TypeReference<HashMap<String, Object>>() {});
            approvalSummaryMap.put("资产管理部门" , assetMap);
            String approvalSummary = objectMapper.writeValueAsString(approvalSummaryMap);

            equScrap.setApprovalSummary(approvalSummary);

            execution.setVariable("equScrap", equScrap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
